﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DCMT
{
    public partial class frmMain : Form
    {
        static string configFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + "DCMT.xml";
        Dictionary<string, Command> commantList = new Dictionary<string, Command>();

        public frmMain()
        {
            InitializeComponent();
            passComputerNameArguments();
            tbWorkstation.SelectionStart = tbWorkstation.Text.Length;
            tbWorkstation.SelectionLength = 0;
            readConfig();
            createForm();
        }

        // struct contains executable and switches
        public struct Command
        {
            public string exe, switches;

            public Command(string s1, string s2)
            {
                exe = s1;
                switches = s2;
            }
        }

        private void passComputerNameArguments()
        {
            string[] args = Environment.GetCommandLineArgs();

            for (int i = 0; i < args.Length; i++)
            {
                // args[0] is exepath, daarna komen eventuele echte argumenten.
                if (i == 1)
                {
                    tbWorkstation.Text = args[i];
                }
            }
        }

        void createForm()
        {
            int i = 1;
            foreach (var name in commantList)
            {
                addRowToLayoutPanel();
                // MessageBox.Show(name.Value.switches);
                addButton(name.Key, i);
                i += 1;
            }
            this.MaximumSize = new System.Drawing.Size(180, this.Height);
            this.MinimumSize = new System.Drawing.Size(180, this.Height);
        }

        void addRowToLayoutPanel()
        {
            // Add row to tablelayoutpanel
            this.tableLayoutPanel1.RowCount += 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
            this.Height += 30;
        }

        void addButton(string name, int position)
        {
            Button button1 = new System.Windows.Forms.Button();
            //button1.Dock = System.Windows.Forms.DockStyle.Fill;
            button1.Location = new System.Drawing.Point(3, 33);
            button1.Name = name;
            button1.Size = new System.Drawing.Size(186, 25);
            button1.TabIndex = 1;
            button1.Text = name;
            button1.UseVisualStyleBackColor = true;
            button1.Click += new System.EventHandler(this.button_Click);

            // Add button to tablelayoutpanel
            this.tableLayoutPanel1.Controls.Add(button1, 0, position);
        }

        void button_Click(object sender, EventArgs e)
        {
            string Button = ((Button)sender).Name;
            string computername;

            if (tbWorkstation.Text.Length == 0)
            {
                computername = "localhost";
            }
            else
            {
                computername = tbWorkstation.Text;
            }

            //Create 
            string completePath = commantList[Button].exe.Replace(".\\", Path.GetDirectoryName(Application.ExecutablePath) + "\\");
            string completeArguments = commantList[Button].switches.Replace("Computername", computername);
            startProgram(completePath, completeArguments);
        }

        void startProgram(string command, string arguments)
        {
            try
            {
                System.Diagnostics.Process.Start(command, arguments);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void readConfig()
        {
            try
            {
                XmlReader xmlReader = XmlReader.Create(configFile);
                while (xmlReader.Read())
                {
                    if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "application"))
                    {
                        if (xmlReader.HasAttributes)
                        {
                            string name = xmlReader.GetAttribute("name");
                            string command = xmlReader.GetAttribute("command");
                            string attributes = xmlReader.GetAttribute("arguments");
                            Command myCommand = new Command(command, attributes);
                            commantList.Add(name, myCommand);
                            // MessageBox.Show(myCommand.exe);
                        }
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                frmAbout about = new frmAbout();
                about.Show();
                return true;    // indicate that you handled this keystroke
            }

            // Call the base class
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
